package cn.elead.chaos.remote.web.remote.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.elead.chaos.core.constant.UserConstants;
import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.framework.web.global.ApiAssert;
import cn.elead.chaos.framework.web.global.ErrorCodeEnum;
import cn.elead.chaos.framework.web.global.response.UnifiedReturn;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.remote.web.remote.domain.SysMenu;
import cn.elead.chaos.remote.web.remote.domain.vo.TreeSelect;
import cn.elead.chaos.remote.web.remote.service.ISysMenuService;
import cn.elead.chaos.sso.core.util.SsoUtils;

/**
 * 菜单信息
 * 
 * @author luopeng
 */
@UnifiedReturn
@RestController
@RequestMapping("/system/menu")
public class RemoteMenuController extends BaseController {
	@Autowired
	private ISysMenuService menuService;

	/**
	 * 获取菜单列表
	 */
	@GetMapping("/list")
	public List<SysMenu> list(SysMenu menu) {
		List<SysMenu> menus = menuService.selectMenuList(menu);
		return menuService.buildMenuTree(menus);
	}

	/**
	 * 根据菜单编号获取详细信息
	 */
	@GetMapping(value = "/{menuId}")
	public SysMenu getInfo(@PathVariable Long menuId) {
		return menuService.selectMenuById(menuId);
	}

	/**
	 * 获取菜单下拉树列表
	 */
	@GetMapping("/treeselect")
	public List<TreeSelect> treeselect(SysMenu dept) {
		List<SysMenu> menus = menuService.selectMenuList(dept);
		return menuService.buildMenuTreeSelect(menus);
	}

	/**
	 * 加载对应角色菜单列表树
	 */
	@GetMapping(value = "/roleMenuTreeselect/{roleId}")
	public List<Integer> roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
		return menuService.selectMenuListByRoleId(roleId);
	}

	/**
	 * 新增菜单
	 */
	@PostMapping
	@Log(title = "菜单管理", businessType = BusinessType.INSERT)
	public int add(@Validated @RequestBody SysMenu menu) {
		if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在"));
		}
		menu.setCreateBy(SsoUtils.getUserName());
		return menuService.insertMenu(menu);
	}

	/**
	 * 修改菜单
	 */
	@PutMapping
	@Log(title = "菜单管理", businessType = BusinessType.UPDATE)
	public int edit(@Validated @RequestBody SysMenu menu) {
		if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在"));
		}
		menu.setUpdateBy(SsoUtils.getUserName());
		return menuService.updateMenu(menu);
	}

	/**
	 * 删除菜单
	 */
	@DeleteMapping("/{menuId}")
	@Log(title = "菜单管理", businessType = BusinessType.DELETE)
	public int remove(@PathVariable("menuId") Long menuId) {
		if (menuService.hasChildByMenuId(menuId)) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("存在子菜单,不允许删除"));
		}
		if (menuService.checkMenuExistRole(menuId)) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("菜单已分配,不允许删除"));
		}
		return menuService.deleteMenuById(menuId);
	}
}